// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        m_coin1:cc.Node,
        m_coin2:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },

    update (dt) {

    },
    addClip:function(){
        
        var animation = this.node.addComponent(cc.Animation);
 
        /* 添加SpriteFrame到frames数组 */
 
        var clip = cc.AnimationClip.createWithSpriteFrames(this.coins, 5);
        clip.name = 'anim_flip';
        if(this.isWhite){
            clip.wrapMode = cc.WrapMode.Normal;
        }else{
            clip.wrapMode = cc.WrapMode.Reverse;
        }
 
        /* 添加关键帧事件 */
        clip.events.push({
            frame: 1,                   // 准确的时间，以秒为单位。这里表示将在动画播放到 1s 时触发事件
            func: 'frameEvent',         // 回调函数名称
            params: [1, 'hello']        // 回调参数
        });
        animation.addClip(clip);

    },
    getIsWhite:function(){
        return !this.isWhite;
    },
    //创建新的硬币
    createNewCoin:function(coinType){
        this.isWhite = (coinType==0);
        this.m_coin1.active = !this.isWhite;
        this.m_coin2.active = this.isWhite;
    },
    //翻转硬币
    flip1:function(){
        console.log("翻转1");
        this.m_coin1.active = false;
        this.m_coin2.active = true;
        if(this.callback){
            this.callback();
        }
    },
    flip2:function(){
        console.log("翻转2");
        this.m_coin1.active = true;
        this.m_coin2.active = false;
        if(this.callback){
            this.callback();
        }
    },
    flipCoin:function(callback){
        //this.callback = callback;
        if(this.isWhite){
            this.m_coin2.active = true;
            this.m_coin1.active = false;
            var animation = this.m_coin2.getComponent(cc.Animation);
            animation.play();
        }else{
            this.m_coin1.active = true;
            this.m_coin2.active = false;
            var animation = this.m_coin1.getComponent(cc.Animation);
            animation.play();
        }
        this.isWhite = !this.isWhite;
            this.node.runAction(cc.sequence(cc.delayTime(0.5),cc.callFunc(()=>{
                //this.m_coin1.active = false;
                //this.m_coin2.active = true;
                if(callback){
                    callback();
                }
            },this)));
        
    },
});
